package com.wm.dao.hibernate;

import java.util.List;

import javax.annotation.Resource;
import org.hibernate.Query;

import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bean.Person;

import com.wm.bean.Dish;
import com.wm.bean.Restaurant;
import com.wm.dao.DishDao;
import com.wm.dao.RestaurantDao;

@Transactional
public class DishDaoHibernate implements DishDao {
	@Resource
	private SessionFactory sessionFactory;
	private int count;

	public DishDaoHibernate(){
		System.out.println("dishDao="+ count);
		count++;
	}
	public List<Dish> getDish(Restaurant rest) {
		
		Query query = sessionFactory.getCurrentSession().createQuery(
		"from Dish  d where d.rest.rid =  '" + rest.getRid() + "'");
		List<Dish> dishList = query.list();
		return dishList;
	}
	public List<Dish> getDishByName(String hql){
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		List<Dish> dishList = query.list();
		return dishList;
	}
	
	public int getDishPagesWithHql(int maxResults, String hql) {
		int count = 0;

		Query query = sessionFactory.getCurrentSession().createQuery(hql);
//		count = ((Number) query.iterate().next())
//        .intValue();
		count = query.list().size();
		double countPage = count / (maxResults * 1.0);

		int countpage = (int) Math.ceil(countPage);
		System.out.println("总共有" + count + "条记录每页显示" + maxResults + "条总共"
				+ countpage + "页");
		return countpage;
	}
	
public List<Dish> getDishsByNameHql(int beginNum, int maxResults, String hql) {
		
//		ArrayList<String> keyWord = (ArrayList<String>)names;
		
		
//		hql = "from Restaurant where 1=1 " + conditionString;
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setFirstResult(beginNum);
		query.setMaxResults(maxResults);
		List<Dish> dList = query.list();
		if (dList.size() > 0) {
			System.out.println(dList.get(0).getName());
		}
		
		return dList;

	}
	/*public List<Dish> getRestaurants(int beginNum, int maxResults) {
		// sessionFactory.getCurrentSession().createQuery("from
		// Restaurant").list();
		Query query = sessionFactory.getCurrentSession().createQuery(
				"from Restaurant");
		query.setFirstResult(beginNum);
		query.setMaxResults(maxResults);
		List<Dish> rList = query.list();
		System.out.println(rList.get(0).getName());
		return rList;

	}

	@Transactional
	public void save(Dish dish) {
		sessionFactory.getCurrentSession().persist(dish);

	}

	public void update(Dish restaurant) {
		// TODO Auto-generated method stub

	}

	public int getRestPages(int maxResults) {
		int count = 0;

		Query query = sessionFactory.getCurrentSession().createQuery(
				"from Dish");
//		count = ((Number) query.iterate().next())
//        .intValue();
		count = query.list().size();
		double countPage = count / (maxResults * 1.0);

		int countpage = (int) Math.ceil(countPage);
		System.out.println("总共有" + count + "条记录每页显示" + maxResults + "条总共"
				+ countpage + "页");
		return countpage;
	}*/

}
